package com.syt.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.syt.commonutils.R;
import com.syt.eduservice.entity.EduCourse;
import com.syt.eduservice.entity.vo.CourseInfoVo;
import com.syt.eduservice.entity.vo.CoursePublicVo;
import com.syt.eduservice.entity.vo.CourseQuery;
import com.syt.eduservice.service.EduCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author SYT
 * @since 2021-03-04
 */
@RestController
@RequestMapping("/eduservice/course")
//@CrossOrigin
public class EduCourseController {
    @Autowired
    private EduCourseService courseService;

    //添加课程基本信息
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        //返回添加之后课程id，为了后面添加大纲使用
        String id = courseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId",id);
    }

    //根据课程Id查询课程基本信息
    @GetMapping("getCourserInfo/{courseId}")
    public R getCourserInfo(@PathVariable("courseId")String courseId){
        CourseInfoVo courseInfoVo = courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }
    //修改课程信息
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        courseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    //根据课程Id查询确认的信息
    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublicCourseInfo(@PathVariable String id){
        CoursePublicVo coursePublicVo=courseService.publicCourseInfo(id);
        return R.ok().data("publicCourse",coursePublicVo);
    }

    //课程最终发布
    //修改课程状态
    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        EduCourse eduCourse=new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal"); //设置课程状态
        courseService.updateById(eduCourse);
        return R.ok();
    }

    //课程列表接口
    // TODO 完善条件查询分页
    @PostMapping("getCourseList/{current}/{limit}")
    public R getCourseList(@PathVariable("current") Long current, @PathVariable("limit") Long limit,@RequestBody(required = false)CourseQuery courseQuery){
        String title = courseQuery.getTitle();
        String status = courseQuery.getStatus();
        QueryWrapper<EduCourse> wrapper=new QueryWrapper<>();
        //创建Page对象
        Page<EduCourse> page=new Page<>(current,limit);
        if (!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        if (!StringUtils.isEmpty(status)){
            wrapper.eq("status",status);
        }
        wrapper.orderByDesc("gmt_create");
        courseService.page(page,wrapper);
        List<EduCourse> list = page.getRecords();
        long total = page.getTotal();
        return R.ok().data("list",list).data("total",total);
    }

    //删除课程
    @DeleteMapping("{courseId}")
    public R deleteCourse(@PathVariable String courseId){
        courseService.removeCourse(courseId);
        return R.ok();
    }
}

